/**
 * Created by Administrator on 2016/7/23 0023.
 */
$(function () {
    var data={};
    data.phone="123";
    var url="/TaskCollection/ProjectClassify/projectClassify.wst";
    $.ajaxSetup({
        async : false
    });
    var count;
    $.post(url,data,function (result) {
        count=result.getAll.length;
        for(var i = 0;i < result.getAll.length;i++){
            var newrow ="<tr><td>"+result.getAll[i].projectClassify_id+"</td><td>"+result.getAll[i].projectClassify_name+"</td><td><input id='chang"+i+"' type='button' value='修改' class='btn'/></td><td><input id='del"+i+"' type='button' value='删除' class='btn'/></td></tr>";
            $("#projectClassifyTable tr:last").after(newrow);

        }
    },"json");
    
    for(var i = 0;i<count;i++){
        (function (m) {
            $("#chang"+i+"").click(function () {
                var projectClassifyID = $("#projectClassifyTable tr:gt(0):eq("+(m+1)+") td:eq(0)").text();
                var projectClassifyName = $("#projectClassifyTable tr:gt(0):eq("+(m+1)+") td:eq(1)").text();
                if(this.value=="确认"){
                    var dataChange = {};
                    dataChange.flag="change";
                    dataChange.id = projectClassifyID;
                    dataChange.name=$("#input"+m).val();
                    var thiseml = this;
                    $.ajaxSetup({
                        async : false
                    });
                    $.post(url,dataChange,function (result) {
                        if(result.msg==true){
                            $("#projectClassifyTable tr:gt(0):eq("+(m+1)+") td:eq(1)").html(dataChange.name);
                            thiseml.value="修改";
                        }else {
                            alert("修改失败");
                        }
                    },"json");
                }else{
                    if(this.value=="修改");{
                        $("#projectClassifyTable tr:gt(0):eq("+(m+1)+") td:eq(1)").html("<input class='inputName' type='text' id='input"+m+"' value='"+projectClassifyName+"'/>");
                        this.value="确认";
                    }
                }
            });
        })(i);
        (function (m) {
            $("#del"+i+"").click(function () {
                var projectClassifyID = $("#projectClassifyTable tr:gt(0):eq("+(m+1)+") td:eq(0)").text();
                //var projectClassifyName = $("#projectClassifyTable tr:gt(0):eq("+(m+1)+") td:eq(1)").text();
                var dataDel = {};
                dataDel.flag = "delete";
                dataDel.id=projectClassifyID;
                $.ajaxSetup({
                    async : false
                });
                $.post(url,dataDel,function (result) {
                   if(result.msg==true){
                       $("#projectClassifyTable tr:gt(0):eq("+(m+1)+")").remove();
                   }else {
                       alert("删除失败");
                   }
                },"json");
            });
        })(i);
        
    }
    $("#add").click(function () {
        var name = $("#addinput").val();
        var dataAdd = {};
        dataAdd.flag="add";
        dataAdd.name=name;
        var url="/TaskCollection/ProjectClassify/projectClassify.wst";
        $.ajaxSetup({
            async : false
        });
        $.post(url,dataAdd,function (result) {
           if(result.msg){
               location.reload(false);
           }else{
               alert("添加失败");
           }
        },"json");
    });
    
});
